package com.pmstation.skin

import xml.XML
import java.net.URL
import java.io._
import javax.imageio.ImageIO

/**
 * Results: true pos/neg, false pos/neg:
 * 5/64    8/7  (84 total) - with learning
 *
 * 2/69    3/10  (84 total) - without learning
 * 3/69    3/9  (84 total) - +100k (500k total) manually selected pixels
 * 7/66    6/5   +300k (700k total) manually selected pixels
 */
object App extends Application {
  val dataDir = "/tmp/nudes/4shared"
  val dataDirNude = "/tmp/nudes/4shared/nude"
  val dataDirNotnude = "/tmp/nudes/4shared/notnude"


  val maxBrFilter = Skin.mkFilter

  // downloadNew()

  dirClear(new File(dataDirNude))
  dirClear(new File(dataDirNotnude))

  for( file <- new File(dataDir).listFiles if file.isFile ){
    if( isNude(file) ){
      cp(file, new File(dataDirNude, file.getName))
      println("nude %s".format(file))
    }else{
      cp(file, new File(dataDirNotnude, file.getName))
      println("notnude %s".format(file))
    }
  }

  def downloadNew(){
    val url = "http://dc188.4shared.com/network/searchXml.jsp?q=jpg&start=0&searchmode=2&sortOrder=1&sortType=2&searchExtention=category:3"
    new File(dataDir).mkdirs()
    val doc = XML.load(new URL(url))
    for(file <- doc \\ "file" ){
      if( (file\"name").text.toLowerCase.endsWith(".jpg") ){
        val furl = (file\"preview-url").text
        val fid = ".4shared.com/img/([^/]+)/".r.findFirstMatchIn(furl).get.group(1)
        val dst = new File(dataDir, fid+".jpg")
        if(!dst.exists
                && !new File(dataDir, "a"+fid+".jpg").exists
                && !new File(dataDir, "b"+fid+".jpg").exists  ){
          println("Downloading %s from %s".format(dst,furl))
          val imgBytes = readAll(new URL(furl).openStream)
          val fo = new FileOutputStream(dst)
          fo.write(imgBytes)
          fo.close()
        }
      }
    }
  }

  def dirClear(dir:File){
    dir.mkdirs()
    dir.listFiles.foreach(_.delete)
  }

  def cp(from:File, to:File){
    val fo = new FileOutputStream(to)
    fo.write(readAll(new FileInputStream(from)))
    fo.close()
  }

  def isNude(f: File)={
    val nud = new NudityDetection( ImageIO.read(f), maxBrFilter.isSkin )
    val nudRes = nud.check()
    nudRes.nude
  }

  /**
     * Get all data from an InputStream as byte array
     * @param r Reader
     * @throws IOException
     * @return String
     */
    def readAll(r: InputStream): Array[Byte] = {
      var sb: ByteArrayOutputStream = new ByteArrayOutputStream
      var buf: Array[Byte] = new Array[Byte](4096)
      var len: Int = 0
      while ((({len = r.read(buf); len})) != -1) {
        sb.write(buf, 0, len)
      }
      return sb.toByteArray
    }

  }
